package gemini.castor.ui.client.page.content.store.catalog;

import gemini.basic.model.Product;
import gemini.castor.ui.client.mvp.container.CastorPlaceContainer;
import gemini.castor.ui.client.mvp.container.CastorPlaceContainer.PresenterGetter;
import gemini.castor.ui.client.mvp.container.CastorWidgetContainerDisplay;
import gemini.castor.ui.client.mvp.container.CastorWidgetContainerPresenter;
import gemini.castor.ui.client.mvp.gin.CastorGinjector;
import gemini.castor.ui.client.utils.PlaceConstants;
import ch.elca.gwt.mvp.client.EventBus;
import ch.elca.gwt.mvp.client.place.Place;
import ch.elca.gwt.mvp.client.place.PlaceRequest;

import com.google.inject.Inject;

public class CatalogPresenter extends
		CastorWidgetContainerPresenter<CatalogPresenter.Display> {

	public interface Display extends CastorWidgetContainerDisplay {
	}

	public static final Place PLACE = new CastorPlaceContainer<CatalogPresenter>(
			PlaceConstants.PLACE_CATALOG, new PresenterGetter<CatalogPresenter>() {
				public CatalogPresenter getPresenter() {
					return CastorGinjector.INSTANCE.getCatalogPresenter();
				}
			});

	@Inject
	public CatalogPresenter(Display display, EventBus eventBus) {
		super(display, eventBus);
		addPresenter(CastorGinjector.INSTANCE.getCategoryCatalogPresenter());
		addPresenter(CastorGinjector.INSTANCE.getProductCatalogPresenter());
	}

	/*
	 * Register listeners on Display actions
	 */
	@Override
	protected void onBind() {
		registerHandlerRegister();
		
		if (product != null) {
			showPresenter(
					CastorGinjector.INSTANCE.getProductCatalogPresenter(),
					product);
		} else {
			showDefaultPresenter();
		}
	}

	private void registerHandlerRegister() {
		registerHandler(getLocalEventBus().addHandler(CatalogEvent.getType(),
				new CatalogHandler() {

					@Override
					public void onViewProducts(CatalogEvent applyEvent) {
						showPresenter(CastorGinjector.INSTANCE
								.getProductCatalogPresenter(),
								getCurrentPresenter().getOutputObject());

					}

				}));
	}

	@Override
	public Place getPlace() {
		return PLACE;
	}
	
	private Product product;
	
	@Override
	public void setInputObject(Object object) {
		this.product = (Product)object;
	}

	@Override
	protected void onPlaceRequest(PlaceRequest request) {
		// TODO Auto-generated method stub
	}
	
	@Override
	public void onUnbind() {
		product = null;
	}
}
